﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Sales;
using ESOL.Common;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageReturnOrder : BaseForm.frmBaseMdiBusinessForm
    {
        #region Vars
        private SalesReturnOrderEntity mSelectedReturnOrder;
        #endregion

        #region Cons
        public frmManageReturnOrder()
        {
            InitializeComponent();
        }
        #endregion

        #region Func
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "SRO", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "SRO", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit2, "UNIT", true);

        }
        private string getCondition()
        {
            string condition = "";

            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND a.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            if (txtApplicationCode.Tag != null && txtApplicationCode.Tag.GetType() == typeof(SalesProdPlanApplicationEntity))
            {
                SalesProdPlanApplicationEntity prod = (SalesProdPlanApplicationEntity)txtApplicationCode.Tag;
                condition += " AND a.APPLICATION_ID = '" + prod.ApplicationId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND a.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = getCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "ORDER_CODE DESC";
            DataTable dt = SalesReturnOrderBs.GetByCondition(condition, orderBy, rowCount);

            gvMain.DataSource = dt;

        }
        private void ResetButtonEnble()
        {
            //btnNew.Enabled = false;
            btnModify.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnDelete.Enabled = false;
            if (mSelectedReturnOrder == null)
            {
                return;
            }
            if (mSelectedReturnOrder.Status == "SRO01")//草稿
            {
                btnModify.Enabled = true;
                btnSubmit.Enabled = true;
                btnDelete.Enabled = true;
            }
            else if(mSelectedReturnOrder.Status=="SRO02")//审核中
            {
                btnApprove.Enabled = true;
            }
            else if (mSelectedReturnOrder.Status == "SRO03")//已审核
            {

            }
            else if (mSelectedReturnOrder.Status == "SRO04")//已拒绝
            {
                btnDelete.Enabled = true;
            }
            else if (mSelectedReturnOrder.Status == "SRO05")//已删除
            { }
            
        }
        private void BindDetail()
        {
            if (mSelectedReturnOrder == null)
            {
                return;
            }
            DataTable dt = SalesReturnItemBs.GetSalesReturnItemsByReturnOrderId(mSelectedReturnOrder.Id).Tables[0];
            if(dt==null||dt.Rows.Count==0)
            {
                gvDetail.DataSource=null;
            }
            gvDetail.DataSource = dt;
        }
        #endregion


        #region Events
        private void frmManageReturnOrder_Load(object sender, EventArgs e)
        {
            BindCombobox();
            BindDataGrid();
        }
        protected override void Click_Query()
        {
            BindDataGrid();
        }
        protected override void Click_New()
        {
            frmMaintainReturnOrder frm = new frmMaintainReturnOrder();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Modify()
        {
            if(mSelectedReturnOrder==null)
            {
                MessageBox.Show("请选择一个退货单","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnOrder frm = new frmMaintainReturnOrder();
            frm.selectedReturnOrder = mSelectedReturnOrder;
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Approve()
        {
            if (mSelectedReturnOrder == null)
            {
                MessageBox.Show("请选择一个退货单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnOrder frm = new frmMaintainReturnOrder();
            frm.selectedReturnOrder = mSelectedReturnOrder;
            frm.ThisActionType = ActionType.Approve;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Submit()
        {
            if (mSelectedReturnOrder == null)
            {
                MessageBox.Show("请选择一个退货单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确认送审？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedReturnOrder.Status = "SRO02";
                mSelectedReturnOrder.Modified = DateTime.Now;
                mSelectedReturnOrder.ModifiedBy = CommonParameter.CurrentUser.PersonId;
                bool successful = SalesReturnOrderBs.UpdateSalesReturnOrder(mSelectedReturnOrder);
                if (successful)
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }
        protected override void Click_Delete()
        {
            if (mSelectedReturnOrder == null)
            {
                MessageBox.Show("请选择一个退货单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确认删除？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedReturnOrder.Status = "SRO05";
                mSelectedReturnOrder.Modified = DateTime.Now;
                mSelectedReturnOrder.ModifiedBy = CommonParameter.CurrentUser.PersonId;
                bool successful = SalesReturnOrderBs.UpdateSalesReturnOrder(mSelectedReturnOrder);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        protected override void Click_View()
        {
            if (mSelectedReturnOrder == null)
            {
                MessageBox.Show("请选择一个退货单", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnOrder frm = new frmMaintainReturnOrder();
            frm.selectedReturnOrder = mSelectedReturnOrder;
            frm.ThisActionType = ActionType.View;
            DialogResult dr = frm.ShowDialog();
        }

        #endregion

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedReturnOrder = new SalesReturnOrderEntity(dr);
            ResetButtonEnble();
            BindDetail();
        }

        private void txtApplicationCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanApplicationQuery frm = new frmMaintainProductPlanApplicationQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtApplicationCode.Text = frm.SelectedProdPlanApplication.ApplicationCode;
                txtApplicationCode.Tag = frm.SelectedProdPlanApplication;
            }
            else
            {
                txtApplicationCode.Text = "";
                txtApplicationCode.Tag = null;
            }
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else 
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        private void btntToInv_Click(object sender, EventArgs e)
        {
            frmManageReturnToInv frm = new frmManageReturnToInv();
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnAccount_Click(object sender, EventArgs e)
        {
            frmManageReturnToInv frm = new frmManageReturnToInv();
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }




    }
}